﻿package morn.core.components
{
    import __AS3__.vec.*;
    import flash.display.*;
    import morn.core.managers.*;

    public class ImageBitmapData extends Object
    {
        private var _bitmaps:Vector.<Bitmap>;
        public var url:String;
        private var _s9Container:Sprite;
        private var _bitmap:Bitmap;
        private var _sizeGrid:Array;
        private var _x:Number;
        private var _y:Number;
        private var _width:Number;
        private var _height:Number;
        private var _smoothing:Boolean;
        private var _bitmapData:BitmapData;
        private var _container:Sprite;
        private var _enabled:Boolean = true;

        public function ImageBitmapData(param1:Sprite)
        {
            this._container = param1;
            return;
        }// end function

        public function get enabled() : Boolean
        {
            return this._enabled;
        }// end function

        public function get bitmapData() : BitmapData
        {
            return this._bitmapData;
        }// end function

        public function set bitmapData(param1:BitmapData) : void
        {
            this._bitmapData = param1;
            if (param1)
            {
                App.render.callLater(this.uptateBitmap);
            }
            return;
        }// end function

        public function get x() : Number
        {
            return this._x;
        }// end function

        public function set x(param1:Number) : void
        {
            this._x = param1;
            if (this._bitmap)
            {
                this._bitmap.x = param1;
            }
            else if (this._s9Container)
            {
                this._s9Container.x = param1;
            }
            return;
        }// end function

        public function get y() : Number
        {
            return this._y;
        }// end function

        public function set y(param1:Number) : void
        {
            this._y = param1;
            if (this._bitmap)
            {
                this._bitmap.y = param1;
            }
            else if (this._s9Container)
            {
                this._s9Container.y = param1;
            }
            return;
        }// end function

        public function get width() : Number
        {
            return isNaN(this._width) ? (this._bitmap ? (this._bitmap.width) : (this._bitmapData ? (this._bitmapData.width) : (0))) : (this._width);
        }// end function

        public function set width(param1:Number) : void
        {
            if (this._width != param1)
            {
                this._width = param1;
                App.render.callLater(this.uptateBitmap);
            }
            return;
        }// end function

        public function get height() : Number
        {
            return isNaN(this._height) ? (this._bitmap ? (this._bitmap.height) : (this._bitmapData ? (this._bitmapData.height) : (0))) : (this._height);
        }// end function

        public function set height(param1:Number) : void
        {
            if (this._height != param1)
            {
                this._height = param1;
                App.render.callLater(this.uptateBitmap);
            }
            return;
        }// end function

        private function uptateBitmap() : void
        {
            var _loc_1:Bitmap = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Array = null;
            var _loc_5:Bitmap = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:BitmapData = null;
            var _loc_13:BitmapData = null;
            var _loc_14:int = 0;
            var _loc_15:int = 0;
            var _loc_16:int = 0;
            var _loc_17:BitmapData = null;
            var _loc_18:BitmapData = null;
            var _loc_19:int = 0;
            var _loc_20:int = 0;
            var _loc_21:int = 0;
            var _loc_22:BitmapData = null;
            var _loc_23:int = 0;
            var _loc_24:int = 0;
            var _loc_25:int = 0;
            var _loc_26:int = 0;
            var _loc_27:int = 0;
            if (!this._enabled)
            {
                return;
            }
            if (!this._sizeGrid)
            {
                if (!this._bitmap)
                {
                    this._bitmap = new Bitmap();
                    this._bitmap.x = this._x;
                    this._bitmap.y = this._y;
                    this._container.addChild(this._bitmap);
                }
                this._bitmap.bitmapData = this._bitmapData;
                if (!isNaN(this._width))
                {
                    this._bitmap.width = this._width;
                }
                if (!isNaN(this._height))
                {
                    this._bitmap.height = this._height;
                }
                if (this._smoothing)
                {
                    this._bitmap.smoothing = this._smoothing;
                }
            }
            else
            {
                if (isNaN(this._width) && this.bitmapData)
                {
                }
                if (isNaN(this._height) && this.bitmapData)
                {
                    this._height = this.bitmapData.height;
                }
                if (this._bitmapData)
                {
                    if (this.url && ImageS9BitmapData.UrlToSizeGrid != null && ImageS9BitmapData.UrlToSizeGrid[this.url])
                    {
                        this._sizeGrid = ImageS9BitmapData.UrlToSizeGrid[this.url];
                    }
                    if (!this._s9Container)
                    {
                        this._s9Container = new Sprite();
                        this._container.addChild(this._s9Container);
                        this._s9Container.x = this._x;
                        this._s9Container.y = this._y;
                    }
                    if (this._bitmaps)
                    {
                        for each (_loc_5 in this._bitmaps)
                        {
                            
                            if (_loc_5 && _loc_5.parent)
                            {
                                _loc_5.parent.removeChild(_loc_5);
                                _loc_5.bitmapData = null;
                            }
                        }
                    }
                    this._bitmaps = new Vector.<Bitmap>;
                    _loc_4 = ImageS9BitmapData.getS9Bitmaps(this._bitmapData, this._sizeGrid, this._width, this._height, true, this.url);
                    if (this._sizeGrid[4])
                    {
                        if (this._height < int(this.sizeGrid[1]) + int(this.sizeGrid[3]) || this._width < int(this.sizeGrid[0]) + int(this.sizeGrid[2]))
                        {
                            _loc_1 = this.addAndSet(this._bitmapData);
                            _loc_1.width = this._width;
                            _loc_1.height = this._height;
                            return;
                        }
                        _loc_8 = _loc_4[0].width + _loc_4[2].width;
                        _loc_9 = _loc_4[0].height + _loc_4[6].height;
                        _loc_10 = this._width > _loc_8 ? (this._width) : (_loc_8);
                        _loc_11 = this._height > _loc_9 ? (this._height) : (_loc_9);
                        if (_loc_10 > _loc_8)
                        {
                            _loc_12 = _loc_4[1];
                            _loc_13 = _loc_4[7];
                            if (_loc_12 != null && _loc_13 != null)
                            {
                                _loc_6 = Math.ceil((this._width - _loc_8) / _loc_12.width);
                                _loc_14 = _loc_12.width;
                                _loc_15 = _loc_13.width;
                                _loc_16 = this._height - _loc_13.height;
                                if (_loc_6 > 50)
                                {
                                    throw new Error("9宫格切的块数太多了。。。");
                                }
                                _loc_7 = 0;
                                while (_loc_7 < _loc_6)
                                {
                                    
                                    this.addAndSet(_loc_12, int(this._sizeGrid[0]) + _loc_7 * _loc_14, 0);
                                    this.addAndSet(_loc_13, int(this._sizeGrid[0]) + _loc_7 * _loc_15, _loc_16);
                                    _loc_7++;
                                }
                            }
                        }
                        if (_loc_11 > _loc_9)
                        {
                            _loc_17 = _loc_4[3];
                            _loc_18 = _loc_4[5];
                            if (_loc_17 != null && _loc_18 != null)
                            {
                                _loc_6 = Math.ceil((this._height - _loc_9) / _loc_17.height);
                                _loc_19 = _loc_17.height;
                                _loc_20 = this._width - _loc_18.width;
                                _loc_21 = _loc_18.height;
                                if (_loc_6 > 50)
                                {
                                    throw new Error("9宫格切的块数太多了。。。");
                                }
                                _loc_7 = 0;
                                while (_loc_7 < _loc_6)
                                {
                                    
                                    this.addAndSet(_loc_17, 0, int(this._sizeGrid[1]) + _loc_7 * _loc_19);
                                    this.addAndSet(_loc_18, _loc_20, int(this._sizeGrid[1]) + _loc_7 * _loc_21);
                                    _loc_7++;
                                }
                            }
                        }
                        if (_loc_10 > _loc_8 && _loc_11 > _loc_9)
                        {
                            _loc_22 = _loc_4[4];
                            if (_loc_22 != null)
                            {
                                _loc_23 = Math.ceil((this._width - _loc_8) / _loc_22.width);
                                _loc_24 = Math.ceil((this._height - _loc_9) / _loc_22.height);
                                if (_loc_23 * _loc_24 > 50)
                                {
                                    throw new Error("9宫格切的块数太多了。。。");
                                }
                                _loc_25 = _loc_22.width;
                                _loc_26 = _loc_22.height;
                                _loc_7 = 0;
                                while (_loc_7 < _loc_24)
                                {
                                    
                                    _loc_27 = 0;
                                    while (_loc_27 < _loc_23)
                                    {
                                        
                                        this.addAndSet(_loc_22, int(this._sizeGrid[0]) + _loc_27 * _loc_25, int(this._sizeGrid[1]) + _loc_7 * _loc_26);
                                        _loc_27++;
                                    }
                                    _loc_7++;
                                }
                            }
                        }
                        if (_loc_4[0])
                        {
                            this.addAndSet(_loc_4[0]);
                        }
                        if (_loc_4[2])
                        {
                            this.addAndSet(_loc_4[2], this._width - _loc_4[2].width);
                        }
                        if (_loc_4[6])
                        {
                            this.addAndSet(_loc_4[6], 0, this._height - _loc_4[6].height);
                        }
                        if (_loc_4[8])
                        {
                            this.addAndSet(_loc_4[8], this._width - _loc_4[8].width, this._height - _loc_4[8].height);
                        }
                    }
                    else
                    {
                        _loc_2 = this._height - this.sizeGrid[1] - this.sizeGrid[3];
                        _loc_3 = this._width - this.sizeGrid[0] - this.sizeGrid[2];
                        if (this._height < int(this.sizeGrid[1]) + int(this.sizeGrid[3]) || this._width < int(this.sizeGrid[0]) + int(this.sizeGrid[2]))
                        {
                            _loc_1 = this.addAndSet(this._bitmapData);
                            _loc_1.width = this._width;
                            _loc_1.height = this._height;
                            return;
                        }
                        if (_loc_4[0])
                        {
                            this.addAndSet(_loc_4[0]);
                        }
                        if (_loc_4[2])
                        {
                            _loc_1 = this.addAndSet(_loc_4[2], this._width - _loc_4[2].width);
                        }
                        if (_loc_4[6])
                        {
                            this.addAndSet(_loc_4[6], 0, this._height - _loc_4[6].height);
                        }
                        if (_loc_4[8])
                        {
                            this.addAndSet(_loc_4[8], this._width - _loc_4[8].width, this._height - _loc_4[8].height);
                        }
                        if (_loc_3 > 0)
                        {
                            if (_loc_4[1])
                            {
                                _loc_1 = this.addAndSet(_loc_4[1], this.sizeGrid[0]);
                                _loc_1.width = _loc_3;
                            }
                        }
                        if (_loc_3 > 0)
                        {
                            if (_loc_4[7])
                            {
                                _loc_1 = this.addAndSet(_loc_4[7], this.sizeGrid[0], this._height - this.sizeGrid[3]);
                                _loc_1.width = _loc_3;
                            }
                        }
                        if (_loc_2 > 0)
                        {
                            if (_loc_4[3])
                            {
                                _loc_1 = this.addAndSet(_loc_4[3], 0, this.sizeGrid[1]);
                                _loc_1.height = _loc_2;
                            }
                        }
                        if (_loc_2 > 0)
                        {
                            if (_loc_4[5])
                            {
                                _loc_1 = this.addAndSet(_loc_4[5], this._width - this.sizeGrid[2], this.sizeGrid[1]);
                                _loc_1.height = _loc_2;
                            }
                        }
                        if (_loc_2 > 0 && _loc_3 > 0)
                        {
                            if (_loc_4[4])
                            {
                                _loc_1 = this.addAndSet(_loc_4[4], this.sizeGrid[0], this.sizeGrid[1]);
                                _loc_1.width = _loc_3;
                                _loc_1.height = _loc_2;
                            }
                        }
                    }
                }
            }
            return;
        }// end function

        public function addAndSet(param1:BitmapData, param2:int = 0, param3:int = 0) : Bitmap
        {
            var _loc_4:Bitmap = null;
            if (param1)
            {
                var _loc_5:* = new Bitmap(param1);
                _loc_4 = new Bitmap(param1);
                this._s9Container.addChild(_loc_5);
                _loc_4.x = param2;
                _loc_4.y = param3;
                this._bitmaps.push(_loc_4);
            }
            return _loc_4;
        }// end function

        public function get sizeGrid() : Array
        {
            return this._sizeGrid;
        }// end function

        public function set sizeGrid(param1:Array) : void
        {
            this._sizeGrid = param1;
            if (param1)
            {
                App.render.callLater(this.uptateBitmap);
            }
            return;
        }// end function

        public function get smoothing() : Boolean
        {
            return this._smoothing;
        }// end function

        public function set smoothing(param1:Boolean) : void
        {
            this._smoothing = param1;
            if (this._bitmap)
            {
                this._bitmap.smoothing = this._smoothing;
            }
            return;
        }// end function

        public function dispose() : void
        {
            var _loc_1:Bitmap = null;
            this._enabled = false;
            this.url = null;
            if (this._bitmaps)
            {
                for each (_loc_1 in this._bitmaps)
                {
                    
                    if (_loc_1 && _loc_1.parent)
                    {
                        _loc_1.parent.removeChild(_loc_1);
                        _loc_1.bitmapData = null;
                    }
                }
                this._bitmaps = null;
            }
            if (this._bitmap)
            {
                if (this._bitmap.parent)
                {
                    this._bitmap.parent.removeChild(this._bitmap);
                }
                this._bitmap.bitmapData = null;
                this._bitmap = null;
            }
            this._sizeGrid = null;
            if (this._s9Container)
            {
                if (this._s9Container.parent)
                {
                    this._s9Container.parent.removeChild(this._s9Container);
                }
                this._s9Container = null;
            }
            this._container = null;
            return;
        }// end function

    }
}
